//
//  ForgetPswNextViewController.m
//  FeiYang
//
//  Created by user on 15/10/14.
//  Copyright © 2015年 feiyang. All rights reserved.
//

#import "ForgetPwdNextViewController.h"
#import "LoginViewController.h"
@interface ForgetPwdNextViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
@property(nonatomic,strong)UITableView  *forgetPswTab;

@property(nonatomic,strong)NSArray      *placeHolderArr;
@property(nonatomic,strong)UIButton    *confirmChangeBtn;
@end

@implementation ForgetPwdNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTheNavigationBar];
    
    
    if (!_placeHolderArr) {
        _placeHolderArr = @[@"请输入新密码",@"请再次输入密码"];
    }
    [self createUI];
    self.view.backgroundColor = [UIColor whiteColor];
}
#pragma mark -------------创建导航栏及相关控件
-(void)createTheNavigationBar
{
    //    自定义navgationBar
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"忘记密码"];
    [self.view addSubview:topView];
    
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self dismissViewControllerAnimated:TRUE completion:^(void){}];
}

-(void)createUI
{
    //    分割线
    
    UILabel *sepLab1 = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 16.0f+50.0f, SCREEN_WIDTH-16.0f, 0.5f)];
    sepLab1.backgroundColor = [UIColor hexStringToColor:kColorGraySperator];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardhide)];
    tapGesture.cancelsTouchesInView = NO;
    [self.forgetPswTab addGestureRecognizer:tapGesture];
    
    [self.forgetPswTab addSubview:sepLab1];
    
    
    [self.view addSubview:self.forgetPswTab];
    [self.forgetPswTab addSubview:self.nextBtn];
}
-(UITableView *)forgetPswTab
{
    if (!_forgetPswTab) {
        
        _forgetPswTab = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _forgetPswTab.delegate = self;
        _forgetPswTab.dataSource = self;
        _forgetPswTab.rowHeight = 50.0f;
        _forgetPswTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _forgetPswTab;
}

//确认修改按钮
-(UIButton *)nextBtn
{
    if (!_confirmChangeBtn) {
        _confirmChangeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,132.0f, SCREEN_WIDTH-20.0f, 40.0f)];
        [_confirmChangeBtn addTarget:self action:@selector(confirmChangeBtnTapped) forControlEvents:UIControlEventTouchUpInside];
        [_confirmChangeBtn setTitle:@"确认修改" forState:UIControlStateNormal];
        _confirmChangeBtn.layer.cornerRadius = 2;
        _confirmChangeBtn.layer.masksToBounds = YES;
        [_confirmChangeBtn setBackgroundColor:[UIColor hexStringToColor:COLOR_VALUE] forState:UIControlStateNormal];
        
        
        
    }
    return _confirmChangeBtn;
}

#pragma mark-tableViewDelegate&datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.placeHolderArr.count;
}
static UITextField *__textField;//记录当前的textField
static UITextField *__textFieldPwd;//记录当前的textField
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        
        UITextField *userTF = [[UITextField alloc] init];
        userTF.tag = 2000;
        userTF.delegate = self;
        userTF.frame = CGRectMake(16.0f, 5.0f, SCREEN_WIDTH-32.0f, 45.0f);
        userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        userTF.font = [UIFont systemFontOfSize:14.0f];
        userTF.secureTextEntry = YES;
        [cell.contentView addSubview:userTF];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    UITextField *text = (UITextField *)[cell.contentView viewWithTag:2000];
    text.placeholder = [_placeHolderArr objectAtIndex:indexPath.row];
    
    
    if (indexPath.row==0) {
        
        
        __textField = text;
    }
    if (indexPath.row==1) {
        __textFieldPwd = text;
        
        
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 16.0f;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)keyboardhide
{
    [__textFieldPwd resignFirstResponder];
    [__textField resignFirstResponder];
}
- (void)confirmChangeBtnTapped
{
    [self.view endEditing:YES];
    
    if ([__textField.text isEqualToString:@""]) {
        ALERT_VIEW(@"请输入新密码");
        return;
    }
    if ([__textFieldPwd.text isEqualToString:@""]) {
        ALERT_VIEW(@"请输入确认密码");
        return;
    }
    if ([__textField.text isEqualToString:__textFieldPwd.text]) {
        
        if ([AccountTools validatePassWord:__textFieldPwd.text]==NO) {
            ALERT_VIEW(@"密码必须为8~32位字符")
        }else{
            
            //    执行
            [[AnimationIndicator sharedIndicator] startAnimation];
            [AppGlobalClass PostDataWithNSstring:RESETPWD withDictionary:@{@"userCd":self.mobile,@"password":__textFieldPwd.text,@"checkCode":self.checkCode} success:^(NSDictionary *dictionary) {
                if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
                {
                    //密码重置成功 去登录界面登录
                    ALERT_VIEW(@"设置成功")
                    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:TRUE completion:^(void){}];
                }else
                {
                    NSString * str = [dictionary objectForKey:@"message"];
                    ALERT_VIEW(str);
                }
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"完成" withType:TRUE];
            } failure:^(NSError *error) {
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"失败" withType:TRUE];
            } networkerror:^(NSString *networkerror) {
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"无网络" withType:TRUE];
            }];
        }
        
    }else{
        ALERT_VIEW(@"两次密码输入不一致");
    }
    
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
